<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Truck_model
 *
 * @author admin
 */
class truck_model extends CI_Model {

    private $_table_driver = 'vpc_driver';
    private $_table_truck = 'vpc_truck';
    private $_table_truck_image = 'vpc_truck_image';
    private $_table_catalog_truck = 'vpc_catalog_truck';
    private $_table_catalog_prefix = 'vpc_catalog_prefix';

    public function get_list_driver() {
        $this->db->select('*');
        $this->db->where('is_deleted', 'N');
        $query = $this->db->get_where($this->_table_driver, array('status_id' => 2, 'is_deleted' => 'N'));
        $arr_driver = array();
        $arr_driver[''] = 'เลือก';
        foreach ($query->result() as $list) {
            $arr_driver[$list->id] = $list->first_name . " " . $list->last_name . " : " . $list->nick_name;
        }
        return $arr_driver;
    }

    public function get_truck_by_id($id) {
        $this->db->select('*');
        $this->db->select('c.id as truck_id');
        $this->db->select('e.prefix_name as prefix_name');
        $this->db->select('a.name as brand_name');
        $this->db->select('b.name as model_name');

        $this->db->where('c.id', $id);
        $this->db->where('c.is_deleted', 'N');

        $this->db->join($this->_table_driver . ' as d', 'd.id = c.driver_id', 'left');
        $this->db->join($this->_table_catalog_truck . ' as a', 'a.id = c.brand_id', 'left');
        $this->db->join($this->_table_catalog_truck . ' as b', 'b.id = c.model_id', 'left');
        $this->db->join($this->_table_catalog_prefix . ' as e', 'e.id = d.prefix_id', 'left');
        $query = $this->db->get($this->_table_truck . ' as c');

        return $query->result();
    }

    public function save_data($license_code, $brand_id, $model_id, $motor_code = NULL, $mile_number = NULL, $driver_id = NULL) {
        $arr_driver = array('license_plate_code' => $license_code,
            'brand_id' => $brand_id,
            'model_id' => $model_id,
            'moter_code' => $motor_code,
            'mile_number' => $mile_number,
            'driver_id' => $driver_id,
            'is_deleted' => 'N',
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $can_save = $this->db->insert($this->_table_truck, $arr_driver);
        return $can_save;
    }

    public function update_data($id, $license_code, $brand_id, $model_id, $motor_code = NULL, $mile_number = NULL, $driver_id = NULL) {
        $arr_driver = array('license_plate_code' => $license_code,
            'brand_id' => $brand_id,
            'model_id' => $model_id,
            'moter_code' => $motor_code,
            'mile_number' => $mile_number,
            'driver_id' => $driver_id,
            'is_deleted' => 'N',
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );
        $arr_where = array('id' => $id, 'is_deleted' => 'N');
        $can_save = $this->db->update($this->_table_truck, $arr_driver, $arr_where);
        return $can_save;
    }

    public function save_image($id, $number, $name) {
        $arr_driver_image = array('truck_id' => $id,
            'file_number' => $number,
            'file_name' => $name,
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $this->db->insert($this->_table_truck_image, $arr_driver_image);
    }

    public function update_image($id, $number, $name) {
        $arr_driver_image = array('truck_id' => $id,
            'file_number' => $number,
            'file_name' => $name,
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );
        $arr_where = array('id' => $id);
        $this->db->update($this->_table_truck_image, $arr_driver_image, $arr_where);
    }

    public function update_image_2($id, $number, $name) {
        $arr_driver_image = array('truck_id' => $id,
            'file_number' => $number,
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );
        $arr_where = array('truck_id' => $id, 'file_name' => $name);
        $this->db->update($this->_table_truck_image, $arr_driver_image, $arr_where);
    }

    public function get_truck_img_by_id($id) {
        $this->db->order_by('file_number', 'asc');
        $query = $this->db->get_where($this->_table_truck_image, array('truck_id' => $id));
        return $query->result();
    }

    public function delete_truck($id) {
        $arr_truck = array('is_deleted' => 'Y',
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );

        $arr_where = array('id' => $id);
        $this->db->update($this->_table_truck, $arr_truck, $arr_where);
    }

    public function delete_truck_image($id, $file_num) {
        $this->db->delete($this->_table_truck_image, array('file_number' => $file_num, 'truck_id' => $id));
    }

    public function get_truck_by_license($license_code) {
        $this->db->where('is_deleted', 'N');
        $query = $this->db->get_where($this->_table_truck, array('license_plate_code' => $license_code));
        return $query->result();
    }

    public function count($license_plate = NULL, $brand = NULL, $model = NULL, $moter_code = NULL) {

        if (!is_null($license_plate) && !empty($license_plate)) {
            $this->db->like('license_plate_code', $license_plate);
        }
        if (!is_null($brand) && !empty($brand)) {
            $this->db->like('brand_id', $brand);
        }
        if (!is_null($model) && !empty($model)) {
            $this->db->like('model_id', $model);
        }
        if (!is_null($moter_code) && !empty($moter_code)) {
            $this->db->where('moter_code', $moter_code);
        }
        $this->db->where('is_deleted', 'N');


        $query = $this->db->get_where($this->_table_truck);
        return count($query->result());
    }

    public function search_list_truck($license_plate = NULL, $brand = NULL, $model = NULL, $moter_code = NULL, $limit = 10, $offset = 0) {

        $this->db->select('*');
        $this->db->select('c.id as truck_id');
        $this->db->select('a.name as brand_name');
        $this->db->select('b.name as model_name');
        if (!is_null($license_plate) && !empty($license_plate)) {
            $this->db->like('c.license_plate_code', $license_plate);
        }
        if (!is_null($brand) && !empty($brand)) {
            $this->db->where('c.brand_id', $brand);
        }
        if (!is_null($model) && !empty($model)) {
            $this->db->where('c.model_id', $model);
        }
        if (!is_null($moter_code) && !empty($moter_code)) {
            $this->db->like('c.moter_code', $moter_code);
        }
        $this->db->where('c.is_deleted', 'N');
        $this->db->join($this->_table_catalog_truck . ' as a', 'a.id = c.brand_id', 'left');
        $this->db->join($this->_table_catalog_truck . ' as b', 'b.id = c.model_id', 'left');
        $query = $this->db->get($this->_table_truck . ' as c', $limit, $offset);

        return $query->result();
    }

}
